import { getRandomNum } from "../../core/utils/utils";
import { attrDefine } from "./attr_define";

/**
 * 
 * ------------------------------        各种计算公式       ------------------------------
 * 
 */

// 基本伤害计算公式，其中计算属性伤害，以及防御参数
export function damageCalc(atk = 0, atkType = null, elementType = null, defPeopleAttr) {
  if(!defPeopleAttr) return 0;
  let def = defPeopleAttr[atkType + 'Def'] || 0;
  if(elementType) {
    let elementDef = defPeopleAttr[elementType + 'Def'];
    if(elementDef != undefined) def = elementDef + def / 10;
  }

  return Math.max(1, atk - def);
}

/**
 * 对伤害进行加工，实现浮动伤害、暴击等
 * @param {*} num 
 * @param {Number} diffNum 是否需要差异值
 * @returns 
 */
export function getDamage({
  // 攻击方式类型
  atkType = attrDefine.blood.key,
  // 是否为元素攻击类型
  elementType = null,
  // 进攻者
  attackPeopleAttr,
  // 防守者
  defPeopleAttr,
  // 额外攻击力
  extraAtk = 0,
  // 力量振幅
  power = 1,
  // 伤害浮动
  diffNum = .1,
}){
  if(!attackPeopleAttr || !defPeopleAttr) return {
    isCrit: false,
    val: 0
  }

  let isCrit = getRandomNum() * 100 < attackPeopleAttr.crit;
  let damage = 0;
  let val = damageCalc((attackPeopleAttr[atkType + 'Atk'] || 0) + extraAtk, atkType, elementType, defPeopleAttr) * power;

  if(diffNum > 0){
    damage = ~~(val * (1 + getRandomNum() * (diffNum * 2) - diffNum));
  } else {
    damage = ~~(val);
  }

  if(isCrit){
    damage *= attackPeopleAttr.critDamage;
  }

  return {
    isCrit,
    val: damage
  }
}